package com.ruoyi.web.controller.order;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;

import com.ruoyi.system.domain.SysAccount;
import com.ruoyi.system.domain.SysOrdersTools;
import com.ruoyi.system.service.ISysOrdersToolsService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysOrders;
import com.ruoyi.system.service.ISysOrdersService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 订单管理Controller
 *
 * @author ruoyi
 * @date 2021-11-19
 */
@RestController
@RequestMapping("/system/orders")
public class SysOrdersController extends BaseController
{
    @Autowired
    private ISysOrdersService sysOrdersService;

    @Autowired
    private ISysOrdersToolsService sysOrdersToolsService;

    /**
     * 查询订单管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:orders:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysOrders sysOrders)
    {
        startPage();
        List<SysOrders> list = sysOrdersService.selectSysOrdersList(sysOrders);
        return getDataTable(list);
    }

    /**
     * 导出订单管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:orders:export')")
    @Log(title = "订单管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SysOrders sysOrders)
    {
        List<SysOrders> list = sysOrdersService.selectSysOrdersList(sysOrders);
        ExcelUtil<SysOrders> util = new ExcelUtil<SysOrders>(SysOrders.class);
        return util.exportExcel(list, "订单管理数据");
    }

    /**
     * 获取订单管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:orders:query')")
    @GetMapping(value = "/{orderId}")
    public AjaxResult getInfo(@PathVariable("orderId") String orderId)
    {
        return AjaxResult.success(sysOrdersService.selectSysOrdersByOrderId(orderId));
    }

    /**
     * 新增订单管理
     */
    @PreAuthorize("@ss.hasPermi('system:orders:add')")
    @Log(title = "订单管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysOrders sysOrders)
    {
        return toAjax(sysOrdersService.insertSysOrders(sysOrders));
    }

    /**
     * 修改订单管理
     */
    @PreAuthorize("@ss.hasPermi('system:orders:edit')")
    @Log(title = "订单管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysOrders sysOrders)
    {
        sysOrders.setOrderStatus("待检查");
        sysOrders.setOrderCustomerService(getUsername());
        return toAjax(sysOrdersService.updateSysOrders(sysOrders));
    }

    /**
     * 删除订单管理
     */
    @PreAuthorize("@ss.hasPermi('system:orders:remove')")
    @Log(title = "订单管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{orderIds}")
    public AjaxResult remove(@PathVariable String[] orderIds)
    {
        return toAjax(sysOrdersService.deleteSysOrdersByOrderIds(orderIds));
    }



//    小程序下单

    @PostMapping("/add")
    public String addOrder(@RequestBody SysOrders sysOrders){

            SimpleDateFormat df = new SimpleDateFormat("yyMMddHHmmss");//设置日期格式
            String date = df.format(new Date());//日期字符串
            sysOrders.setOrderId("As"+date);

            sysOrders.setOrderStatus("未派单");
            if (sysOrders.getOrderIsGuarantee().equals("否")){
                sysOrders.setOrderGuaranteeImg("https://6c65-lei2000-5g122bbv56606aaf-1308029367.tcb.qcloud.la/topdodo_1.jpg?sign=13d0b0b8ec27b392c9b0bc7e2d38b50f&t=1639451549");
                sysOrders.setOrderIspay("未支付");
            }else {
                sysOrders.setOrderIspay("保修");
            }

        sysOrdersService.insertSysOrders(sysOrders);


        return "添加成功";
    }


    //查找用户全部订单
    @PostMapping("/myorders")
    @ResponseBody
    public List<SysOrders> getMyorders(@RequestBody SysOrders sysOrders){

        return  sysOrdersService.selectSysOrdersByorderPhone(sysOrders.getOrderPhone());

    }

    //按条件查找用户订单
    @PostMapping("/myorders/condition")
    @ResponseBody
    public List<SysOrders> getMyorders_condition(@RequestBody SysOrders sysOrders){

        String  orderPhone =sysOrders.getOrderPhone();
        String  orderStatus =sysOrders.getOrderStatus();
        return  sysOrdersService.selectSysOrders_condition_ByorderPhone(orderPhone,orderStatus);

    }


//    按条件查找用户订单价格
    @PostMapping("/myorders/price")
    @ResponseBody
    public List<SysOrders> getMyorders_Price(@RequestBody SysOrders sysOrders){

        String  orderPhone =sysOrders.getOrderPhone();
        String  orderStatus =sysOrders.getOrderStatus();

        return  sysOrdersService.selectSysOrders_Price(orderPhone,orderStatus);

    }


    //    工单详情
    @PostMapping("/myworkorders/detail")
    @ResponseBody
    public SysOrders getMyworkorders_Detail(@RequestBody SysOrders sysOrders){

        return  sysOrdersService.selectSysOrdersByOrderId(sysOrders.getOrderId());

    }

    //按条件查找用户订单价格
//    @PostMapping("/myorders/price")
//    @ResponseBody
//    public String getMyorders_Price(){
//
//        String  orderPhone ="18520239539";
//        String  orderStatus ="已完成";
//
//        List<SysOrders> listsys  =sysOrdersService.selectSysOrders_Price(orderPhone,orderStatus);
//        System.out.println("listsys = " + listsys);
//
//        return  "0";
//
//    }











    //订单详情
    @RequestMapping("/myorders/detail")
    @ResponseBody
    public SysOrders getMyorders_detail(String orderid){

        return  sysOrdersService.selectSysOrders_and_SystoolsOrders(orderid);

    }
    //订单详情
    @PostMapping("/myorders/baoxiu")
    @ResponseBody
    public SysOrders getMyorders_baoxiu(@RequestBody SysOrders sysOrders){
        return  sysOrdersService.selectSysOrdersByOrderId(sysOrders.getOrderId());

    }

    //查找员工全部工单
    @PostMapping("/myworkorders")
    @ResponseBody
    public List<SysOrders> getMyworkorders(@RequestBody SysOrders sysOrders){



        return  sysOrdersService.selectSysOrdersByMaintenancePhone(sysOrders.getOrderMaintenancePhone());

    }

    //按条件查找维修组工单
    @PostMapping("/myworkorders/condition")
    @ResponseBody
    public List<SysOrders> getMyworkorders_condition(@RequestBody SysOrders sysOrders){

        String  orderMaintenancePhone =sysOrders.getOrderMaintenancePhone();
        String  orderStatus =sysOrders.getOrderStatus();
        System.out.println(orderMaintenancePhone);
        System.out.println(orderStatus);
        return  sysOrdersService.selectSysOrders_condition(orderMaintenancePhone,orderStatus);

    }

    //评价
    @PostMapping("/myorders/evaluate")
    public String order_evaluate(@RequestBody SysOrders sysOrders){


        SysOrdersTools sysOrdersTools =sysOrdersToolsService.selectSysOrdersToolsByOrderId(sysOrders.getOrderId());
        sysOrdersTools.setOrderStatus("已完成");
        sysOrdersToolsService.updateSysOrdersTools(sysOrdersTools);

        sysOrders.setOrderStatus("已完成");
        sysOrdersService.updateSysOrders(sysOrders);

        return  "success";

    }


    //时间
    @PostMapping("/myorders/repairtime")
    public String order_repairtime(@RequestBody SysOrders sysOrders){

        SysOrdersTools sysOrdersTools =sysOrdersToolsService.selectSysOrdersToolsByOrderId(sysOrders.getOrderId());
        sysOrdersTools.setOrderStatus("待维修");
        sysOrdersToolsService.updateSysOrdersTools(sysOrdersTools);

        sysOrders.setOrderStatus("待维修");
        sysOrdersService.updateSysOrders(sysOrders);


        return  "success";

    }



    //上传维修视频
    @PostMapping("/myorders/addvideo")
    public String order_video(@RequestBody SysOrders sysOrders){
        System.out.println("sysOrders.getOrderRepairvideo() = " + sysOrders.getOrderRepairvideo());
        sysOrders.setOrderStatus("待验收");
        SysOrdersTools sysOrdersTools =sysOrdersToolsService.selectSysOrdersToolsByOrderId(sysOrders.getOrderId());
        sysOrdersTools.setOrderStatus("待验收");

        sysOrdersService.updateSysOrders(sysOrders);
        sysOrdersToolsService.updateSysOrdersTools(sysOrdersTools);


        return  "success";

    }


    //取消订单
    @RequestMapping("/cancelorder")
    public String cancelOrder(@RequestBody SysOrders sysOrders){
        System.out.println("sysOrders.getOrderId() = " + sysOrders.getOrderId());
        sysOrders.setOrderStatus("取消订单");
        sysOrdersService.updateSysOrders(sysOrders);

        return "success";

    }

//    @PostMapping("/upload")
//    public String upload(MultipartFile file) throws IOException {
//        String originalFilename = file.getOriginalFilename();  // 获取源文件的名称
//        // 定义文件的唯一标识（前缀）
//
//        String rootFilePath = System.getProperty("user.dir") + "/files/" + flag + "_" + originalFilename;  // 获取上传的路径
//        FileUtil.writeBytes(file.getBytes(), rootFilePath);  // 把文件写入到上传的路径
//        return "http://" + ip + ":" + port + "/files/" + flag;  // 返回结果 url
//    }

    //取消订单




}